package com.doyens.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doyens.auth.mapper.SysRoleMapper;
import com.doyens.auth.mapper.SysUserMapper;
import com.doyens.auth.mapper.SysUserRoleMapper;
import com.doyens.auth.service.SysRoleService;
import com.doyens.auth.service.SysUserRoleService;
import com.doyens.auth.service.SysUserService;
import com.doyens.model.system.SysRole;
import com.doyens.model.system.SysUserRole;
import com.doyens.vo.system.AssginRoleVo;
import com.mysql.cj.log.Log;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Autowired //自动按照类型注入
    private SysRoleService sysRoleService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Override
    public Map<String, Object> findRole(int userId) {
        // 1.查询所有角色  sys_role
        List<SysRole> allRoleList = sysRoleService.list(); //[1:校长  2.总监 3.讲师 4.班主任]


        // 2.查询中间表  sys_user_role
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRole::getUserId,userId);
        List<SysUserRole> sysUserRoleList = sysUserRoleService.list(wrapper);

        //[1:校长  2.总监 ]
        // 定义一个集合 存入已经分配的角色
        ArrayList<SysRole> assginRoleList = new ArrayList();

        for (SysUserRole sysUserRole : sysUserRoleList) {
            Long roleId = sysUserRole.getRoleId();
            for (SysRole sysRole : allRoleList) {
                if(sysRole.getId() == roleId){
                    assginRoleList.add(sysRole);
                }
            }
        }

        // 将两个小集合 存入大集合
        HashMap<String, Object> map = new HashMap<>();
        map.put("allRolesList",allRoleList);
        map.put("assginRoleList",assginRoleList); //{allRolesList:{所有角色}，assginRoleList：{已经分配的角色}}

        return map;
    }

    @Override
    public void doAssign(AssginRoleVo assginRoleVo) {
        // 先删除
        LambdaQueryWrapper<SysUserRole> wapper = new LambdaQueryWrapper<>();
        Long userId = assginRoleVo.getUserId();
        wapper.eq(SysUserRole::getUserId, userId);
        sysUserRoleService.remove(wapper);

        // 再添加  1    3            1     4
        List<Long> roleIdList = assginRoleVo.getRoleIdList();
        for (Long roleId : roleIdList) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(userId);
            sysUserRole.setRoleId(roleId);
            sysUserRoleService.save(sysUserRole);
        }
    }
}
